#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1

cpulimit_get() {
	local enabled
	local limit
	local exename
	config_get enabled $1 enabled
	[ $enabled -gt 0 ] || return 1
	config_get limit $1 limit
	[ -z "$limit" ] && return 1
	[ "$limit" -gt 0 -a "$limit" -le 100 ] || return 1
	config_get exename $1 exename
	[ -z "$exename" ] && return 1
	exename="$(basename "$exename")"
	procd_open_instance
	procd_set_param command cpulimit
	procd_append_param command -l $limit
	procd_append_param command -e "$exename"
	procd_set_param stderr 1
	procd_set_param nice -20
	procd_set_param respawn
	procd_close_instance
}

start_service() {
	config_load cpulimit
	config_foreach cpulimit_get list
}

service_triggers(){
	procd_add_reload_trigger "cpulimit"
}
